﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class Department : BasePage
    {
        private IDepartmentBLL departmentBLL = IocContainer.Instance.Resolve<IDepartmentBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!IsPostBack)
            {
                ControlAuthority(new List<WebControl> { btnhidDelete, btnUpdate, btnDelete, btnAddSubNode,btnAddSameLevel }, 24);
                litTreeView.Text = BuildTree();
            }


        }

        private string BuildTree()
        {
            DataTable dtDepartment = departmentBLL.GetAllDepartment();

            return GenerateTreeHtmlFragment(dtDepartment, 0);
        }

        private string GenerateTreeHtmlFragment(DataTable dtDepartment, int parentId)
        {
            StringBuilder result = new StringBuilder("");
            if (dtDepartment != null && dtDepartment.Rows.Count > 0)
            {
                DataRow[] drRoots = dtDepartment.Select("ParentId = " + parentId);
                foreach (DataRow row in drRoots)
                {
                    result.AppendFormat("<ul><li><a href=\"javascript:void(0)\" title=\"{0}\" tname=\"{1}\" tvalue=\"{2}\" tlevel=\"{4}\">{3}</a>", row["Description"], row["ParentId"], row["DepartmentId"], row["DepartmentName"], row["Level"]);

                    int deptId = 0;
                    int.TryParse(row["DepartmentId"].ToString(), out deptId);
                    result.Append(GenerateTreeHtmlFragment(dtDepartment, deptId));
                    result.Append("</li></ul>");
                }
            }
            return result.ToString();
        }


        protected void btnhidDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int deptId = 0;
                int.TryParse(this.txtHidDeptId.Value.Trim(), out deptId);
                if (deptId >= 0)
                {
                    departmentBLL.Delete(deptId);
                    litTreeView.Text = BuildTree();
                }
                else
                {
                    Alert("请选择要删除的节点！");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("错误提示：" + exception.Message);
            }
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                Skyworth.CRM.Service.Entity.Department dept = new Skyworth.CRM.Service.Entity.Department();
                dept.DepartmentName = this.txtDepartmentName.Text;
                dept.Description = this.txtDescription.Text;
                dept.ParentId = Convert.ToInt16(this.txtHidParentId.Value);
                dept.Level = Convert.ToInt16(this.txtHidLevel.Value);
                dept.DepartId = Convert.ToInt16(this.txtHidDeptId.Value);

                departmentBLL.Update(dept);
                litTreeView.Text = BuildTree();
                //Alert("请选择要删除的节点！");
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }

        protected void btnAddSameLevel_Click(object sender, EventArgs e)
        {
            try
            {
                Skyworth.CRM.Service.Entity.Department dept = new Skyworth.CRM.Service.Entity.Department();
                dept.DepartmentName = this.txtDepartmentName.Text;
                dept.Description = this.txtDescription.Text;
                dept.ParentId = Convert.ToInt16(this.txtHidParentId.Value);
                dept.Level = Convert.ToInt16(this.txtHidLevel.Value);

                departmentBLL.Add(dept);
                litTreeView.Text = BuildTree();
                //Alert("请选择要删除的节点！");
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }

        }

        protected void btnAddSubNode_Click(object sender, EventArgs e)
        {
            try
            {
                Skyworth.CRM.Service.Entity.Department dept = new Skyworth.CRM.Service.Entity.Department();
                dept.DepartmentName = this.txtDepartmentName.Text;
                dept.Description = this.txtDescription.Text;
                dept.ParentId = Convert.ToInt16(this.txtHidDeptId.Value);
                dept.Level = Convert.ToInt16(this.txtHidLevel.Value) + 1;

                departmentBLL.Add(dept);
                litTreeView.Text = BuildTree();
                //Alert("请选择要删除的节点！");
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }


    }
}
